<?php
/**
 * Created by PhpStorm.
 * User: yifeng
 * Date: 2018/3/20
 * Time: 17:29
 * 个人博客信息管理
 */

namespace app\blog\controller;


use app\common\controller\Base;
use app\common\model\User;
class Index extends Base
{
    public function index(){
//        $username=decrypt(cookie('name'),SALT);
//        return "欢迎{$username}登录易风博客系统！";
        $result=User::where('account', session('username'))->field('logo,blogtitle,blogname,host,info,content')->find();
        $result = $result->toArray();
        if(request()->isPost()){
            $data=input('post.');
//            获取文件对象
            // 获取表单上传文件 例如上传了001.jpg
            $file = request()->file('logo');
            $fileinfo=Base::upload($file,$result['logo']);
            if($fileinfo['code']==1){
                $data['logo']=$fileinfo['path'];
            }
            $validate = validate('User');
            if($data['blogtitle']!=''){
                if(!$validate->scene('blogtitle')->check($data)){
                    $this->error($validate->getError(),'blog/Index/index');
                }
            }
            if($data['host']!=''){
                if(!$validate->scene('host')->check($data)){
                    $this->error($validate->getError(),'blog/Index/index');
                }
            }
            $res = User::where('account', session('username'))
                ->update($data);
            if($res!==false){
                $this->success('修改成功','blog/Index/index');
            }
        }
        $this->assign('user',$result);
        return view();
    }
    //个人资料
    public function personal(){
        $result=User::where('account', session('username'))->field('face,account,nick,email,uid')->find();
        $result = $result->toArray();
        if(request()->isPost()){
            $data=input('post.');

            $file = request()->file('face');
            $fileinfo=Base::upload($file,$result['face']);
            if($fileinfo['code']==1){
                $face=$fileinfo['path'];
            }else{
                $face="";
            }
            $newdata=[
                'nick'=>$data['nick'],
                'face'=>$face
            ];
            $validate = validate('User');
            if($result['nick']!==$data['nick']){
                if(!$validate->scene('nick')->check($data)){
                    $this->error($validate->getError(),'blog/Index/personal');
                }
            }
            $res=User::where('uid',$result['uid'])->update($newdata);
            if($res!==false){
                $this->success('修改成功','blog/Index/personal');
            }
        }
        $this->assign('personal',$result);
        return view();
    }
    //密码修改
    public function setpasswd(){
        if(request()->isPost()){
            $data=input('post.');
            $result=User::setpasswd($data);
            if($result['code']==1){
                $this->success($result['msg'],'blog/Index/setpasswd');
            }else{
                $this->error($result['msg'],'blog/Index/setpasswd','',2);
            }
        }
        return view();
    }
}